# include <reg52.h>
# define Adjust(Item,Max,Min) do{unsigned char temp;\
    temp = (Item >> 4) * 10;temp += Item&0x0f;\
    if(++temp==Max)temp = Min;\
    Item = temp/10 * 16;Item += temp % 10;}while(0)

sbit KeyFunction = P3^6;
sbit KeyAdjust= P3^7;
unsigned char code du[]={0x10,6,9,8,8,9,6,0};
unsigned char code year[]={8,31,2,15,10,31,2,0};
unsigned char code month[]={15,9,15,9,15,9,19,0};
unsigned char code day[]={15,9,9,15,9,9,15,0};
unsigned char Status,Count;
bit Flash;
unsigned char DateTime[7];
unsigned char DisplayBuffer[32];
unsigned char Week[7][3]={"SUN","MON","TUS","WED","THR","FRI","SAT"};
unsigned int Temperature;
void delayms(unsigned int x);
extern void DS1302GetDateTime(unsigned char *);
extern void DS1302SetDateTime(unsigned char *);
extern void LcdInit();
extern void LcdWriteData(unsigned char);
extern void LcdWriteCommand(unsigned char);
extern void LcdDelay(unsigned char);
extern unsigned int ReadTemperature(void);


void Display()
{
    unsigned char i;
    DisplayBuffer[0]='0'+2;
    DisplayBuffer[1]='0'+0;
    DisplayBuffer[2]='0'+DateTime[6]/16;
    DisplayBuffer[3]='0'+DateTime[6]%16;
    DisplayBuffer[4]=1;    
    DisplayBuffer[5]='0'+DateTime[4]/16;
    DisplayBuffer[6]='0'+DateTime[4]%16;
    DisplayBuffer[7]=2;
    DisplayBuffer[8]='0'+DateTime[3]/16;
    DisplayBuffer[9]='0'+DateTime[3]%16;
    DisplayBuffer[10]=3;
    DisplayBuffer[11]=' ';
    DisplayBuffer[12]=Week[DateTime[5]-1][0];
    DisplayBuffer[13]=Week[DateTime[5]-1][1];
    DisplayBuffer[14]=Week[DateTime[5]-1][2];
    DisplayBuffer[15]=' ';
    DisplayBuffer[16]='0'+DateTime[2]/16;
    DisplayBuffer[17]='0'+DateTime[2]%16;
    DisplayBuffer[18]=':';
    DisplayBuffer[19]='0'+DateTime[1]/16;
    DisplayBuffer[20]='0'+DateTime[1]%16;
    DisplayBuffer[21]=':';
    DisplayBuffer[22]='0'+DateTime[0]/16;
    DisplayBuffer[23]='0'+DateTime[0]%16;
    DisplayBuffer[24]=' ';
    
    if (Status && Flash)
    {
        if(Status == 1)
            DisplayBuffer[0]=DisplayBuffer[1]=DisplayBuffer[2]=DisplayBuffer[3]=' ';
        else if(Status == 2)
            DisplayBuffer[5]=DisplayBuffer[6]=' ';
        else if(Status == 3)
            DisplayBuffer[8]=DisplayBuffer[9]=' ';
        else if(Status == 4)
            DisplayBuffer[12]=DisplayBuffer[13]=DisplayBuffer[14]=' ';
        else if(Status == 5)
            DisplayBuffer[16]=DisplayBuffer[17]=' ';
        else if(Status == 6)
            DisplayBuffer[19]=DisplayBuffer[20]=' ';
        else if(Status == 7)
            DisplayBuffer[22]=DisplayBuffer[23]=' ';
    }
    DisplayBuffer[25]=' ';
    DisplayBuffer[26]=DisplayBuffer[27]=' ';
    DisplayBuffer[28]='0'+Temperature %100/10;
    DisplayBuffer[29]='.';
    DisplayBuffer[30]='0'+Temperature%10;
    DisplayBuffer[31]=0;
    if(Temperature & 0x8000)
    {
        DisplayBuffer[25]='-';
        Temperature = ~Temperature;
        Temperature++;
    }
    Temperature = (int)(Temperature * 0.625);
    if(Temperature/1000)
        DisplayBuffer[26]='0'+Temperature/1000;
    if(Temperature/100)
        DisplayBuffer[27]='0'+Temperature%1000/100;
    LcdWriteCommand(0x80);
    for(i=0;i<16;i++)
        LcdWriteData(DisplayBuffer[i]);
    LcdWriteCommand(0xC0);
    for(;i<32;i++)
        LcdWriteData(DisplayBuffer[i]);  
}

void KeyFunctionProcess()
{
    if(KeyFunction) return;
    delayms(20);
    if(KeyFunction) return;
    if(++Status==8) Status=0;
    while(!KeyFunction);
}

void KeyAdjustProcess()
{
    if(KeyAdjust)   return;
    delayms(20);
    if(KeyAdjust)   return;
    switch(Status)
    {case 1: Adjust(DateTime[6],100,0);
        break;
    case 2: Adjust(DateTime[4],13,1);
        break;
    case 3: Adjust(DateTime[3],32,1);
        break;
    case 4: Adjust(DateTime[5],8,1);
        break;
    case 5:Adjust(DateTime[2],24,0);
        break;
    case 6: Adjust(DateTime[1],60,0);
        break;
    case 7: DateTime[0]=0;
        break;
    }
    DS1302SetDateTime(DateTime);
    while(!KeyAdjust);
}

void main()
{
    unsigned char i;
    LcdInit();
    LcdDelay(100);
    LcdWriteCommand(0x40);
    for(i=0;i<32;i++)
    LcdWriteData(du[i]);
    TMOD=1;
    ET0=1;
    EA=1;
    TR0=1;
    while(1)
    {
        KeyFunctionProcess();
        KeyAdjustProcess();
    }
}

void t0m() interrupt 1
{
    TH0= 15536/256;
    TL0= 15536%256;
    if(++Count ==5)
    {
        Count = 0;
        Flash = ~Flash;
        DS1302GetDateTime(DateTime);
        Temperature = ReadTemperature();
        Display();
    }
}

void delayms(unsigned int x)
{
	unsigned char i;
	while(x--)
		for(i=0;i<123;i++);	


}